package com.mdd.service.auth.impl;

import com.alibaba.fastjson.JSON;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.domain.AlipayFundAuthOperationDetailQueryModel;
import com.alipay.api.domain.AlipayFundAuthOrderAppFreezeModel;
import com.alipay.api.domain.AlipayFundAuthOrderUnfreezeModel;
import com.alipay.api.request.*;
import com.alipay.api.response.*;
import com.mdd.service.auth.PreAuthorizationService;
import com.mdd.validate.AuthParams;
import com.mdd.validate.FreezeAppOrder;
import com.mdd.validate.FreezeCancelParams;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

/**
 * 预授权签约
 * <p>
 * {@code @Description}
 * @Author 幻速
 * {@code @Date} 2023/10/30 15:26
 **/
@Slf4j
@Service
public class PreAuthorizationServiceImpl implements PreAuthorizationService {

    @Override
    public AlipayFundAuthOrderAppFreezeResponse aliPayFreeze(AlipayClient alipayClient, AlipayFundAuthOrderAppFreezeModel alipayFundAuthOrderAppFreezeModel, String notifyUrl) throws AlipayApiException {
        log.info("AlipayFundAuthOrderAppFreezeRequest  params------->"+JSON.toJSONString(alipayFundAuthOrderAppFreezeModel));
        log.info("AlipayFundAuthOrderAppFreezeRequest  notifyUrl------->"+notifyUrl);
        AlipayFundAuthOrderAppFreezeRequest request = new AlipayFundAuthOrderAppFreezeRequest();
        // 设置异步通知
        request.setNotifyUrl(notifyUrl);
        // 设置整体请求参数
        request.setBizModel(alipayFundAuthOrderAppFreezeModel);
        // 使用sdkExecute方法发起请求
        AlipayFundAuthOrderAppFreezeResponse response = alipayClient.sdkExecute(request);
        if (response.isSuccess()) {
            System.out.println(" 调用成功");
            //APP支付唤起冻结，orderStr 入参到 alipay.payV2(orderStr, true )接口
            //H5支付唤起冻结，orderStr 入参到 ap.tradePay接口
            //小程序唤起冻结 orderStr入参到 my.tradePay接口
        } else {
            System.out.println("调用失败");
        }
        return response;
    }

    @Override
    public AlipayFundAuthOperationDetailQueryResponse authQuery(AlipayClient alipayClient,   AlipayFundAuthOperationDetailQueryModel alipayFundAuthOperationDetailQueryModel) throws AlipayApiException {
        log.info("alipayFundAuthOperationDetailQueryModel params------>"+JSON.toJSONString(alipayFundAuthOperationDetailQueryModel));
        AlipayFundAuthOperationDetailQueryRequest request = new AlipayFundAuthOperationDetailQueryRequest();
        // 设置整体请求参数
        request.setBizModel(alipayFundAuthOperationDetailQueryModel);
        // 使用execute方法发起请求
        AlipayFundAuthOperationDetailQueryResponse response = alipayClient.certificateExecute(request);
        log.info("AlipayFundAuthOperationDetailQueryResponse----->"+JSON.toJSONString(response));
        return response;
    }

    @Override
    public AlipayFundAuthOperationCancelResponse authCancel(AlipayClient alipayClient, FreezeCancelParams freezeCancelParams) throws AlipayApiException {
        log.info("AlipayFundAuthOperationCancelRequest----->"+JSON.toJSONString(freezeCancelParams));
        AlipayFundAuthOperationCancelRequest request = new AlipayFundAuthOperationCancelRequest();
        // 设置整体请求参数
        request.setBizContent(JSON.toJSONString(freezeCancelParams));
        // 使用execute方法发起请求
        AlipayFundAuthOperationCancelResponse response = alipayClient.certificateExecute(request);
        log.info("AlipayFundAuthOperationCancelResponse---->"+response);
        return response;
    }

    @Override
    public AlipayFundAuthOrderUnfreezeResponse unfreeze(AlipayClient alipayClient,AlipayFundAuthOrderUnfreezeModel model) throws AlipayApiException {
        log.info("AlipayFundAuthOrderUnfreezeRequest----->"+JSON.toJSONString(model));
        AlipayFundAuthOrderUnfreezeRequest request = new AlipayFundAuthOrderUnfreezeRequest();

        // 设置整体请求参数
        request.setBizModel(model);
        // 使用execute方法发起请求
        AlipayFundAuthOrderUnfreezeResponse response = alipayClient.certificateExecute(request);
        log.info("AlipayFundAuthOrderUnfreezeResponse--->"+response);
        return response;
    }

    @Override
    public AlipayFundAuthOrderVoucherCreateResponse authVoucherCreate(AlipayClient alipayClient,FreezeAppOrder freezeAppOrder) throws AlipayApiException {
        log.info("AlipayFundAuthOrderVoucherCreateRequest------>"+JSON.toJSONString(freezeAppOrder));
        AlipayFundAuthOrderVoucherCreateRequest request = new AlipayFundAuthOrderVoucherCreateRequest();
        // 设置整体请求参数
        request.setBizContent(JSON.toJSONString(freezeAppOrder));
        // 使用execute方法发起请求
        AlipayFundAuthOrderVoucherCreateResponse response = alipayClient.certificateExecute(request);
        log.info("AlipayFundAuthOrderVoucherCreateResponse---->"+JSON.toJSONString(response));
        return response;
    }

    @Override
    public AlipayFundAuthOrderFreezeResponse authOrderFreeze(AlipayClient alipayClient,FreezeAppOrder freezeAppOrder,String notifyUrl) throws AlipayApiException {
        log.info("AlipayFundAuthOrderFreezeRequest------>"+JSON.toJSONString(freezeAppOrder));
        log.info("notifyUrl---->"+notifyUrl);
        AlipayFundAuthOrderFreezeRequest request = new AlipayFundAuthOrderFreezeRequest();
        // 设置异步地址，仅支持http/https，公网可访问
        request.setNotifyUrl(notifyUrl);
        // 设置整体请求参数
        request.setBizContent(JSON.toJSONString(freezeAppOrder));
        // 使用execute方法发起请求
        AlipayFundAuthOrderFreezeResponse response = alipayClient.certificateExecute(request);
        log.info("AlipayFundAuthOrderFreezeResponse----->"+response);
        return response;
    }
}
